crush-core
High-performance parallel compression library with a pluggable algorithm system.
Overview
crush-core is the core library for the Crush compression toolkit. It provides:
- Plugin-based architecture - extensible compression via the
CompressionAlgorithmtrait - Intelligent plugin selection - automatic scoring based on throughput and compression ratio weights
- Timeout protection - configurable timeouts prevent runaway operations
- Cooperative cancellation - lock-free, async-signal-safe cancellation tokens
- CRC32 integrity checks - automatic checksum generation and validation
- File metadata preservation - mtime and Unix permissions stored in the archive
- RAII resource cleanup - partial output files automatically removed on failure
Installation
[]
= "0.1.1"
Quick Start
use ;
// Initialize the plugin registry (call once at startup)
init_plugins.expect;
// Compress
let data = b"Hello, Crush!";
let compressed = compress.expect;
// Decompress
let result = decompress.expect;
assert_eq!;
Advanced Usage
Compression Options
use ;
use Duration;
init_plugins?;
// Select a specific plugin
let options = default
.with_plugin
.with_timeout;
let compressed = compress_with_options?;
// Automatic selection weighted toward speed
let weights = new?;
let options = default.with_weights;
let compressed = compress_with_options?;
Cancellation
use ;
use AtomicCancellationToken;
use Arc;
init_plugins?;
let token = new;
let options = default
.with_cancel_token;
// Cancel from another thread or signal handler
// token.cancel();
let result = compress_with_options;
Inspection
use ;
init_plugins?;
let compressed = compress?;
let info = inspect?;
println!;
println!;
println!;
Plugin Discovery
use ;
init_plugins?;
for plugin in list_plugins
Writing a Custom Plugin
Implement the CompressionAlgorithm trait and register with the linkme distributed slice:
use ;
use Result;
use distributed_slice;
use Arc;
use AtomicBool;
;
static MY_PLUGIN: &dyn CompressionAlgorithm = &MyPlugin;
Built-in Plugins
| Plugin | Algorithm | Throughput | Ratio | Description |
|---|---|---|---|---|
deflate |
DEFLATE (RFC 1951) | 200 MB/s | 0.35 | Standard DEFLATE via flate2 |
Archive Format
Crush archives use a 16-byte header followed by optional CRC32 and metadata:
[Header 16B] [CRC32 4B] [Metadata (optional)] [Compressed payload]
Header layout (little-endian):
Bytes 0-3: Magic number ([0x43, 0x52, version, plugin_id])
Bytes 4-11: Original size (u64)
Byte 12: Flags (bit 0 = CRC32 present, bit 1 = metadata present)
Bytes 13-15: Reserved
Error Handling
All operations return crush_core::Result<T>, with errors organized by category:
CrushError::Plugin- plugin not found, duplicate magic, operation failureCrushError::Validation- invalid header, CRC mismatch, corrupted dataCrushError::Timeout- operation exceeded timeout, plugin panicCrushError::Io- I/O errorsCrushError::Cancelled- operation cancelled via cancellation token
Development
License
MIT